package com.ut.demo.slice;

import com.ut.demo.ResourceTable;
import com.dingmouren.paletteimageview.PaletteImageView;
import com.dingmouren.paletteimageview.util.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.PopupDialog;

public class MainAbilitySlice extends BaseAbilitySlice implements Component.ClickedListener, Slider.ValueChangedListener {

    private Button btnMore;
    private PaletteImageView paletteImageView;
    private Slider sImgRadius, sShadowRadius,sOffsetX,sOffsetY;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }
    public void initView() {
        paletteImageView = findComponent(ResourceTable.Id_palette);
        sImgRadius = findComponent(ResourceTable.Id_img_radius_progress);
        sShadowRadius = findComponent(ResourceTable.Id_shadow_radius_progress);
        sOffsetX = findComponent(ResourceTable.Id_x_progress);
        sOffsetY = findComponent(ResourceTable.Id_y_progress);
        paletteImageView.setShadowColor(0xff795548);
        sImgRadius.setValueChangedListener(this);
        sShadowRadius.setValueChangedListener(this);
        sOffsetX.setValueChangedListener(this);
        sOffsetY.setValueChangedListener(this);
        btnMore = findComponent(ResourceTable.Id_more);
        btnMore.setClickedListener(this);
    }


    private void showDialog(Component component) {
        PopupDialog popupDialog = new PopupDialog(this, component);
        String[] titles = {"sample one", "sample two"};
        DirectionalLayout toastLayout = new DirectionalLayout(this);
        toastLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        toastLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        toastLayout.setOrientation(Component.VERTICAL);
        for (String name : titles) {
            Text text = new Text(this);
            text.setText(name);
            text.setTextSize(50);
            text.setPadding(60, 30, 60, 30);
            text.setClickedListener(v -> {
                popupDialog.hide();
                switch (((Text)v).getText()){
                    case "sample one":
                        present(new GalleryAbilitySlice(), new Intent());
                        break;
                    case "sample two":
                        present(new GridListPictureAbilitySlice(), new Intent());
                        break;
                }
            });
            toastLayout.addComponent(text);
        }

        popupDialog.setCustomComponent(toastLayout);
        popupDialog.show();

    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        showDialog(component);
    }

    @Override
    public void onProgressUpdated(Slider slider, int progress, boolean b) {
        LogUtil.i("onProgressUpdated: %d, value: %d, boolean: %b", slider.getId(), progress, b);
        switch (slider.getId()){
            case ResourceTable.Id_img_radius_progress:
                paletteImageView.setPaletteRadius(progress*500/100);
                break;
            case ResourceTable.Id_shadow_radius_progress:
                paletteImageView.setPaletteShadowRadius(progress);
                break;
            case ResourceTable.Id_x_progress:
                paletteImageView.setPaletteShadowOffset(progress,0);
                break;
            case ResourceTable.Id_y_progress:
                paletteImageView.setPaletteShadowOffset(0,progress);
                break;
        }
    }


    @Override
    public void onTouchStart(Slider slider) {
        LogUtil.i("onTouchStart: %d", slider.getId());
    }

    @Override
    public void onTouchEnd(Slider slider) {
        LogUtil.i("onTouchEnd: %d", slider.getId());
    }
}
